﻿using System;
using UniRx;

public abstract class AIStateBase
{
    /// <summary>
    /// 所属AI
    /// </summary>
    public AIBase ai { get; set; }

    /// <summary>
    /// AI状态名
    /// </summary>
    public string name => m_Name ?? (m_Name = GetName());
    protected abstract string GetName();
    private string m_Name;

    /// <summary>
    /// 是否为持续型状态
    /// </summary>
    public virtual bool isLoop { get; } = true;

    /// <summary>
    /// 优先级。数值越大，优先级越高，相同优先级之间不互相覆盖
    /// </summary>
    public virtual int priority { get; } = UnitDefine.STATE_PRIORITY_DEFAULT;

    /// <summary>
    /// 是否已完成，用于标记非持续性状态是否完成
    /// </summary>
    public bool isEnd { get; set; } = false;

    /// <summary>
    /// Update更新流
    /// </summary>
    public IObservable<Unit> updateStream => m_UpdateStream ?? (m_UpdateStream = InitUpdateStream());
    protected virtual IObservable<Unit> InitUpdateStream() => ai.entity.lifeCycle.GetLifeCycleStream(UnitLifeCycle.LifeType.AIExecute).AsUnitObservable();
    private IObservable<Unit> m_UpdateStream;

    /// <summary>
    /// 初始化
    /// </summary>
    protected virtual void OnInit() { }
    public void Init()
    {
        OnInit();
    }

    /// <summary>
    /// 进入状态
    /// </summary>
    protected virtual void OnEnter() { }
    public void Enter()
    {
        OnEnter();
    }

    /// <summary>
    /// 更新状态
    /// </summary>
    protected virtual void OnExecute() { }
    public void Execute()
    {
        OnExecute();
    }

    /// <summary>
    /// 退出状态
    /// </summary>
    protected virtual void OnExit() { }
    public void Exit()
    {
        OnExit();
    }
}
